package com.tanjun.common.enums;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

@Getter
@AllArgsConstructor
public enum FileTypeEnum  {

    IMAGE(1, "图片", 5 * 1024 * 1024L, Arrays.asList("png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif", "svg")),
    VIDEO(2, "视频", 100 * 1024 * 1024L, Arrays.asList("mp4", "avi", "mkv", "flv", "mov", "wmv")),
    DOC(3, "Word", 20 * 1024 * 1024L, Arrays.asList("doc", "docx"));
    @JsonValue
    private Integer type;
    private String name;//名称
    private Long maxSize; //最大大小(b)
    private List<String> suffix; //允许后缀


    /**
     * 枚举数据库存储值
     */
    public Integer getValue() {
        return type;
    }

    /**
     * 判断文件后缀是否满足要求
     *
     * @param fileName 文件名称
     * @return bool
     */
    public boolean matchSuffix(String fileName) {
        Assert.notNull(fileName, "fileName cannot be null");
        if (fileName.contains(".")
                && this.suffix.contains(fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase())) {
            return true;
        }
        return false;
    }
}
